package com.sds.sweng.swat.miplatform.ui.design.utils;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.eclipse.gef.palette.CombinedTemplateCreationEntry;
import org.eclipse.jface.resource.ImageDescriptor;

import com.sds.sweng.swat.miplatform.ui.design.MiplatformUIDesignerActivator;
import com.sds.sweng.swat.miplatform.ui.design.models.FavoritesLoader;
import com.sds.sweng.swat.miplatform.ui.design.models.MiplatformComponent;
import com.sds.sweng.swat.miplatform.ui.design.models.area.GeneralAreaTemplate;
import com.sds.sweng.swat.miplatform.ui.design.models.area.GridAreaTemplate;
import com.sds.sweng.swat.miplatform.ui.design.models.area.SearchAreaTemplate;
import com.sds.sweng.swat.miplatform.ui.design.models.area.TabAreaTemplate;
import com.sds.sweng.swat.miplatform.ui.design.palettes.FavoriteBean;
import com.sds.sweng.swat.miplatform.ui.design.palettes.MiplatformDesignPaletteFactory;

public class PaletteUtils {

	private static List<FavoriteBean> favorites = new ArrayList<FavoriteBean>();
	private static String storedId = MiplatformUIDesignerActivator.PLUGIN_ID + "_favorites";

	public static void addFavorite(String favoriteName, MiplatformComponent favoriteModel) {
		addFavorite(favoriteName, favoriteModel, true);
	}

	public static List<FavoriteBean> getFavorites() {
		return favorites;
	}

	public static void addFavorite(String favoriteName, MiplatformComponent favoriteModel, boolean isNew) {

		FavoriteBean favorite = new FavoriteBean();
		favorite.setFavoriteName(favoriteName);
		favorite.setFavoriteModel(favoriteModel);
		favorites.add(favorite);

		if (isNew)
			saveFavorites();

		ImageDescriptor imageDescriptor = null;
		if (favoriteModel instanceof SearchAreaTemplate) {
			imageDescriptor = ImageDescriptor.createFromFile(MiplatformDesignPaletteFactory.class, "images/search.gif");
		} else if (favoriteModel instanceof GeneralAreaTemplate) {
			imageDescriptor = ImageDescriptor.createFromFile(MiplatformDesignPaletteFactory.class, "images/general.gif");
		} else if (favoriteModel instanceof GridAreaTemplate) {
			imageDescriptor = ImageDescriptor.createFromFile(MiplatformDesignPaletteFactory.class, "images/grid.gif");
		} else if (favoriteModel instanceof TabAreaTemplate) {
			imageDescriptor = ImageDescriptor.createFromFile(MiplatformDesignPaletteFactory.class, "images/tab.gif");
		} else {
			imageDescriptor = ImageDescriptor.createFromFile(MiplatformDesignPaletteFactory.class, "images/favorite.gif");
		}

		CombinedTemplateCreationEntry entry = new CombinedTemplateCreationEntry(
				favoriteName,
				"",
				favoriteModel.getClass(),
				new FavoritesLoader(favoriteModel),
				imageDescriptor, imageDescriptor);

		MiplatformDesignPaletteFactory.getFavoritesDrawer().add(entry);
	}

	private static void saveFavorites() {
		String jsonModel = StringUtils.EMPTY;
		try {
			jsonModel = ModelUtils.toJsonFavorites(favorites);
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}

		MiplatformUIDesignerActivator.getDefault().getPreferenceStore().putValue(storedId, jsonModel);
	}

	public static List getStoredFavorites() {
		String storedValue = MiplatformUIDesignerActivator.getDefault().getPreferenceStore().getString(storedId);

		if (StringUtils.isEmpty(storedValue)) {
			return new ArrayList<FavoriteBean>();
		} else {
			try {
				favorites = ModelUtils.loadFavorites(storedValue);
				return favorites;
			} catch (Exception e) {
				e.printStackTrace();
				return new ArrayList<FavoriteBean>();
			}
		}
	}

	public static void deleteFavorites(int[] selectionIndices) {
		for (int i=selectionIndices.length-1;i>=0;i--) {
			favorites.remove(selectionIndices[i]);
			CombinedTemplateCreationEntry entry = (CombinedTemplateCreationEntry) MiplatformDesignPaletteFactory.getFavoritesDrawer().getChildren().get(selectionIndices[i]);
			MiplatformDesignPaletteFactory.getFavoritesDrawer().remove(entry);
		}

		saveFavorites();
	}
}
